package in.muton.edushare.pages.publicpages.deptpages;

import in.muton.edushare.common.UserFilter;
import in.muton.edushare.processing.objects.DeptDataHandler;
import in.muton.edushare.wicket.models.InnerList;
import in.muton.panels.paginator.EntityListing;

import java.util.List;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.SubmitLink;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.vo.Users;

public class DeptPeoplePublic extends DeptBasePublic {

	public DeptPeoplePublic(PageParameters params) {
		super(params);
		if (params.getString("2").equals("students"))
			students = true;
		Link<Void> l1 = new Link<Void>("allPeople") {
			private static final long serialVersionUID = 4112756843574343695L;

			@Override
			public void onClick() {
				searchString = "";
			}
		};
		add(l1);
		l1.add(new Label("allCaption", new Model<String>() {
			@Override
			public String getObject() {
				if (students)
					return "All Students";
				return "All Faculty";
			}

			private static final long serialVersionUID = 7619805542870992453L;
		}));

		Form<Void> f = new Form<Void>("searchForm");
		add(f);
		f.add(new TextField<String>("searchTerm", new PropertyModel<String>(
				this, "searchString")));
		f.add(new SubmitLink("searchLink"));

		add(new EntityListing<Users>("userBadges", new InnerList<Users>() {
			private static final long serialVersionUID = -56856143743214954L;

			@Override
			public List<IModel<Users>> getObject() {
				if (students)
					return UserFilter.filterByString(
							new DeptDataHandler(getDeptModel()).getStudents(),
							searchString).getObject();
				return UserFilter.filterByString(
						new DeptDataHandler(getDeptModel()).getTeachers(),
						searchString).getObject();
			}
		}, this));

	}

	@Override
	public String getPageName(PageParameters params) {
		if (params.getString("2").equals("students"))
			return "DeptStudentsPublic";
		return "DeptFacultyPublic";
	}

	private boolean students = false;
	private String searchString = "";
}
